package com.dcy.file.service;

import com.dcy.common.base.service.DcyBaseService;
import com.dcy.common.enums.AdminApiErrorCode;
import com.dcy.common.exception.BusinessException;
import com.dcy.common.model.PageModel;
import com.dcy.common.model.PageResult;
import com.dcy.file.convert.FileInfoConvert;
import com.dcy.file.dao.FileInfoDao;
import com.dcy.file.model.FileInfo;
import com.dcy.file.util.FileExtNameUtil;
import com.dcy.file.vo.in.FileInfoSearchInVO;
import com.dcy.file.vo.out.FileInfoListOutVO;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;

/**
 * @Author：dcy
 * @Description:
 * @Date: 2021/4/20 14:21
 */
@RequiredArgsConstructor
@Service
public class FileInfoService extends DcyBaseService {

    private final FileHandler fileHandler;
    private final FileInfoDao fileInfoDao;
    private final FileInfoConvert fileInfoConvert = FileInfoConvert.INSTANCE;

    /**
     * 获取表格数据
     *
     * @param fileInfoSearchInVO
     * @param pageModel
     * @return
     */
    public PageResult<FileInfoListOutVO> pageListByEntity(FileInfoSearchInVO fileInfoSearchInVO, PageModel pageModel) {
        return toPageResult(fileInfoDao.pageListByEntity(fileInfoConvert.toFileInfo(fileInfoSearchInVO), pageModel).convert(fileInfoConvert::toOut));
    }

    /**
     * 文件上传
     *
     * @param file
     * @return
     */
    public FileInfoListOutVO upload(MultipartFile file) {
        if (!FileExtNameUtil.checkFileExtName(file.getOriginalFilename())) {
            throw new BusinessException(AdminApiErrorCode.FILE_UPLOAD_FORMAT_ERROR);
        }
        // 文件上传白名单
        FileInfo fileInfo = fileHandler.uploadFile(file);
        return fileInfoConvert.toOut(fileInfoDao.save(fileInfo) ? fileInfo : null);
    }

    /**
     * 文件上传多个
     *
     * @param files
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public List<FileInfoListOutVO> uploadFiles(MultipartFile[] files) {
        if (Arrays.stream(files).anyMatch(multipartFile -> !FileExtNameUtil.checkFileExtName(multipartFile.getOriginalFilename()))) {
            throw new BusinessException(AdminApiErrorCode.FILE_UPLOAD_FORMAT_ERROR);
        }
        List<FileInfo> fileInfos = fileHandler.uploadFiles(files);
        fileInfoDao.saveBatch(fileInfos);
        return fileInfoConvert.toOutList(fileInfos);
    }

    /**
     * 删除文件
     *
     * @param id
     */
    public void deleteFile(String id) {
        FileInfo fileInfo = fileInfoDao.getById(id);
        fileHandler.deleteFile(fileInfo);
    }

    /**
     * 下载文件
     *
     * @param fileName
     * @param filePath
     * @param response
     */
    public void downLoad(String fileName, String filePath, HttpServletResponse response) {
        fileHandler.downLoad(fileName, filePath, response);
    }

}
